from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, and_, or_, text
from sqlalchemy.sql import Select
from typing import List, Optional, Dict, Any
from module_admin.entity.do.employee_salary_do import EmployeeSalary
from module_admin.entity.vo.employee_salary_vo import EmployeeSalaryQueryModel
from utils.page_util import PageUtil


class EmployeeSalaryDao:
    """
    员工薪资模块数据访问层
    """

    @classmethod
    async def get_employee_salary_list(cls, db: AsyncSession, salary_query: EmployeeSalaryQueryModel, data_scope_sql: str, is_page: bool = False):
        """
        获取员工薪资列表信息

        :param db: orm对象
        :param salary_query: 查询参数对象
        :param data_scope_sql: 数据权限对应的查询sql语句
        :param is_page: 是否开启分页
        :return: 员工薪资列表信息对象
        """
        query = (
            select(EmployeeSalary)
            .where(
                EmployeeSalary.del_flag == '0',
                EmployeeSalary.employee_id == salary_query.employee_id if salary_query.employee_id else True,
                EmployeeSalary.last_year_daily_wage == salary_query.last_year_daily_wage if salary_query.last_year_daily_wage else True,
                EmployeeSalary.current_year_daily_wage == salary_query.current_year_daily_wage if salary_query.current_year_daily_wage else True,
                eval(data_scope_sql),
            )
            .order_by(EmployeeSalary.id)
            .distinct()
        )
        salary_list = await PageUtil.paginate(db, query, salary_query.page_num, salary_query.page_size, is_page)
        return salary_list

    @classmethod
    async def get_employee_salary(cls, db: AsyncSession, salary_id: int):
        """
        获取单个员工薪资详情
        :param db: orm对象
        :param salary_id: 薪资ID
        :return: 员工薪资详情
        """
        stmt = select(EmployeeSalary).where(
            and_(
                EmployeeSalary.id == salary_id,
                EmployeeSalary.del_flag == '0'
            )
        )
        result = await db.execute(stmt)
        return result.scalar_one_or_none()

    @classmethod
    async def add_employee_salary(cls, db: AsyncSession, salary: EmployeeSalaryQueryModel):
        """
        新增员工薪资信息
        :param db: orm对象
        :param salary: 新增薪资对象
        :return: 新增结果
        """
        salary_dict = {
            'employee_id': salary.employee_id,
            'last_year_daily_wage': salary.last_year_daily_wage,
            'current_year_daily_wage': salary.current_year_daily_wage,
            'del_flag': '0',
            'create_by': salary.create_by,
            'create_time': salary.create_time,
            'update_by': salary.update_by,
            'update_time': salary.update_time,
        }
        db_salary = EmployeeSalary(**salary_dict)
        db.add(db_salary)
        await db.flush()
        return db_salary

    @classmethod
    async def update_employee_salary(cls, db: AsyncSession, salary_id: int, salary: EmployeeSalaryQueryModel):
        """
        修改员工薪资信息
        :param db: orm对象
        :param salary_id: 薪资ID
        :param salary: 修改薪资对象
        :return: 修改结果
        """
        salary_dict = {
            'employee_id': salary.employee_id,
            'last_year_daily_wage': salary.last_year_daily_wage,
            'current_year_daily_wage': salary.current_year_daily_wage,
            'update_by': salary.update_by,
            'update_time': salary.update_time,
        }
        stmt = select(EmployeeSalary).where(
            and_(
                EmployeeSalary.id == salary_id,
                EmployeeSalary.del_flag == '0'
            )
        )
        result = await db.execute(stmt)
        salary_obj = result.scalar_one_or_none()
        
        if salary_obj:
            for key, value in salary_dict.items():
                if value is not None:
                    setattr(salary_obj, key, value)
            await db.flush()
            return True
        return False

    @classmethod
    async def delete_employee_salary(cls, db: AsyncSession, salary_id: int):
        """
        删除员工薪资信息
        :param db: orm对象
        :param salary_id: 薪资ID
        :return: 删除结果
        """
        stmt = select(EmployeeSalary).where(
            and_(
                EmployeeSalary.id == salary_id,
                EmployeeSalary.del_flag == '0'
            )
        )
        result = await db.execute(stmt)
        salary_obj = result.scalar_one_or_none()
        
        if salary_obj:
            salary_obj.del_flag = '1'
            await db.flush()
            return True
        return False 